package weaver.interfaces.xfjt.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xfjt.utils.XfjtUtil;
import weaver.soa.workflow.request.RequestInfo;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 流程归档，根据物品名称，更新【办公用品库存】，库存表中不存在的插入，存在的累计加库存值
 * @ModifyLog:
 **/
public class WeaBgypSglcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        User user = requestInfo.getRequestManager().getUser();
        int userid = user.getUID();
        writeLog(">>>>>WeaBgypSglcAction>>>>>requestid"+requestid);
        RecordSet rs = new RecordSet();
        try {
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            rs.executeQuery("select * from "+tablename+"_dt1 where mainid=?",mainid);
            while (rs.next()) {
                Map requestMainMap = new HashMap<>();
                //名称浏览
                String mc = Util.null2String(rs.getString("mc"));
                int sgsl = Util.getIntValue(rs.getString("sgsl"),0);
                boolean flag = XfjtUtil.isRepeatGoods(mc);
                writeLog("是否存在="+flag);
                if (flag) {
                    requestMainMap.put("mc",mc);
                    //类别
                    requestMainMap.put("lb",Util.null2String(rs.getString("lb")));
                    //单位
                    requestMainMap.put("dw",Util.null2String(rs.getString("dw")));
                    //申购数量
                    requestMainMap.put("kc",sgsl);
                    //名称
                    requestMainMap.put("mcwb",XfjtUtil.getGoodsName(mc));
                    //类型
                    requestMainMap.put("lx",Util.null2String(rs.getString("lx")));
                    writeLog("requestMainMap="+requestMainMap.toString());
                    XfjtUtil.insertModelData("uf_bgypkc",userid,requestMainMap);
                }else {
                    rs.executeUpdate("update uf_bgypkc set kc=kc+"+sgsl+" where mc=?",mc);
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
